package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.dao.cargo.ExtEproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ExtCproductDao extCproductDao;



    /**
     * 购销合同分页
     * @param contractExample 分页查询的参数
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {
        //设置开始页与也买你大小
        PageHelper.startPage(pageNum, pageSize);
        //查询全部
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);
        return pageInfo;
    }

    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        return contractDao.selectByExample(contractExample);
    }

    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Contract contract) {
        contract.setId(UUID.randomUUID().toString());
        //工厂创建时间
        contract.setCreateTime(new Date());
        //工厂的更新时间
        contract.setUpdateTime(new Date());
        contractDao.insertSelective(contract);
    }

    @Override //保存购销合同
    public void update(Contract contract) {

        contract.setCreateTime(new Date());
        contract.setUpdateTime(new Date());
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void delete(String id) {
        //删除合同包含的货物
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdEqualTo(id);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(contractProductExample);
        for (ContractProduct contractProduct : contractProducts) {
            contractProductDao.deleteByPrimaryKey(contractProduct.getId());
        }
        //删除合同中货物包含的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        for (ExtCproduct extCproduct : extCproducts) {
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        contractDao.deleteByPrimaryKey(id);

    }
    //大区经理查看购销合同
    @Override
    public PageInfo<Contract> findPageDeptId(String companyId, String deptId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
       List<Contract> contractList= contractDao.findByDeptId(companyId,deptId);
       return new PageInfo<>(contractList);
    }

    @Override
    public List<Contract> findThreeDay() {
        return contractDao.findThreeDay();
    }
}
